/*
 * <<
 *  Davinci
 *  ==
 *  Copyright (C) 2016 - 2019 EDP
 *  ==
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  >>
 *
 */

package com.sdkj.dspservice.modules.report.service.impl;

import com.sdkj.dspservice.common.base.service.impl.CrudServiceImpl;
import com.sdkj.dspservice.modules.report.entity.tem.TableInfo;
import com.sdkj.dspservice.modules.report.utils.SourceException;
import com.sdkj.dspservice.modules.report.utils.SourceUtils;
import com.sdkj.dspservice.modules.report.utils.SqlUtils;
import com.sdkj.dspservice.common.enums.LogNameEnum;
import com.sdkj.dspservice.modules.report.dao.SourceDao;
import com.sdkj.dspservice.modules.report.entity.Source;
import com.sdkj.dspservice.modules.report.service.SourceService;
import com.sdkj.dspservice.modules.report.utils.entity.JdbcSourceInfo;
import com.sdkj.dspservice.modules.report.utils.hive.HiveUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;

/**
 * 数据源service实现类
 * @author tm
 */
@Slf4j
@Service("sourceService")
@Transactional(readOnly = true)
public class SourceServiceImpl extends CrudServiceImpl<SourceDao, Source> implements SourceService {

    private static final Logger optLogger = LoggerFactory.getLogger(LogNameEnum.BUSINESS_OPERATION.getName());

    @Autowired
    private SqlUtils sqlUtils;

    /**
     * 测试数据源
     *
     * @param source
     * @return
     */
    @Override
    public boolean testSource(Source source) throws SQLException {
        return SourceUtils.testSource(source.getDatabaseUrl(), source.getDatabaseUsername(), source.getDatabasePassword());
    }

    /**
     * 获取Source 的 db
     *
     * @param id
     * @return
     */
    @Override
    public List<String> getDatabaseNameList(String id) {
        Source source = dao.selectById(id);
        List<String> dbList = null;
        if(source != null){
            try {
                dbList = sqlUtils.init(new JdbcSourceInfo(source)).getDatabases();
            } catch (SourceException e) {
                e.printStackTrace();
            }
        }
        return dbList;
    }

    /**
     * 获取数据表名称列表
     *
     * @param id 数据源id
     * @return
     */
    @Override
    public List<String> getTableNameList(String id) {
        // 获取数据源
        Source source = dao.selectById(id);
        if(Source.DATABASE_TYPE_HIVE.equals(source.getDatabaseType())){
            return  new HiveUtils(source.getDatabaseUrl(), source.getDatabaseUsername(), source.getDatabasePassword()).getTableNameList();
        }
        // sqlUtils
        SqlUtils sqlUtilsInit = sqlUtils.init(new JdbcSourceInfo(source));
        // 获取当前数据库
        String databaseName = sqlUtilsInit.getDatabaseName();
        // 获取数据库表名列表
        List<String> tableNameList = sqlUtilsInit.getTableNameList(databaseName);
        return tableNameList;
    }

    /**
     * 获取表结构
     *
     * @param id
     * @return
     */
    @Override
    public TableInfo getTableInfo(String id, String tableName) {
        // 获取数据源
        Source source = dao.selectById(id);
        TableInfo tableInfo;
        if(source.getDatabaseType().indexOf(Source.DATABASE_TYPE_HIVE) != -1){
            tableInfo = new TableInfo();
            tableInfo.setColumns(new HiveUtils(source).getTableColumnList(tableName));
        }else {
            // sqlUtils
            SqlUtils sqlUtilsInit = sqlUtils.init(new JdbcSourceInfo(source));
            // 获取当前数据库
            String databaseName = sqlUtilsInit.getDatabaseName();
            // 获取表结构
            tableInfo = sqlUtilsInit.getTableInfo(databaseName, tableName);
        }
        return tableInfo;
    }

}
